package com.example.lin.internalstorage_demo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Arrays;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.m1:
                testWriteFile();
                break;
            case R.id.m2:
                testReadFile();
                break;
            case R.id.m3:
                testDeleteFile();
                break;
            case R.id.m4:
                testFileList();
                break;
            case R.id.m5:
                testGetFilesDir();
                break;
            case R.id.m6:
                testGetDir();
                break;
            case R.id.m7:
                testGetCacheDir();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void testGetCacheDir() {

        File cacheDir = getCacheDir();
        Log.i(TAG, "testGetCacheDir: " + cacheDir.getAbsolutePath());


    }

    private void testGetDir() {
        File filesDir = getDir("abc", MODE_PRIVATE);//自己观察文件的路径
        Log.d(TAG, "testGetDir: " + filesDir.getAbsolutePath());
    }

    private void testGetFilesDir() {
        File filesDir = getFilesDir();//自己观察文件的路径
        Log.d(TAG, "testGetFilesDir: " + filesDir.getAbsolutePath());

    }

    private void testFileList() {
        String[] strings = fileList();//返回内部存储的所有文件的名称的数组
        Toast.makeText(this, Arrays.toString(strings), Toast.LENGTH_SHORT).show();

    }

    private void testDeleteFile() {
        boolean b = deleteFile("xyz.txt");
        Toast.makeText(this, "b:" + b, Toast.LENGTH_SHORT).show();
    }

    private void testReadFile() {
        //打开内部存储的指定文件输入流
        try (FileInputStream fis = openFileInput("xyz.txt");
             BufferedReader br = new BufferedReader(new InputStreamReader(fis));) {
            String s = br.readLine();
            Toast.makeText(this, "s:" + s, Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    private void testWriteFile() {
        //打开内部存储的指定文件输出流  (将永久性的保存,系统不会自动清楚这些文件)
        try (FileOutputStream fos = openFileOutput("xyz.txt", MODE_PRIVATE);
             BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos))) {
            bw.write("狂风啊,暴雨!");
            Toast.makeText(this, "成功!", Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


}
